package condominio.servlet.residente;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import condominio.excepcion.DAOExcepcion;
import condominio.negocio.GestionResidentes;

/**
 * Servlet implementation class ResidenteModificarServlet
 */
@WebServlet("/ResidenteModificarServlet")
public class ResidenteModificarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResidenteModificarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String nombre = request.getParameter("nombre");
		String apellidopaterno = request.getParameter("apellidopaterno");
		String apellidomaterno = request.getParameter("apellidomaterno");
		String codigoresidente = request.getParameter("codigoresidente");
		String strfechanacimiento = request.getParameter("fechanacimiento");
		SimpleDateFormat parse = new SimpleDateFormat("dd-MM-yyyy");
		java.util.Date date=null;
		try {
			date = parse.parse(strfechanacimiento);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date fechanacimiento = new java.sql.Date(date.getTime());
		String correo = request.getParameter("correo");
		String password = request.getParameter("password");
		
		GestionResidentes negocio = new GestionResidentes();
		try {
			negocio.actualizar(codigoresidente, correo, password, nombre, apellidopaterno, apellidomaterno, fechanacimiento);
			response.sendRedirect(request.getContextPath()	+ "/Residentes.jsp");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("Error.jsp");
			rd.forward(request, response);
		}
		
	}

}
